<?php

namespace Yearnfar\Bundle\CmsBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Yearnfar\Bundle\CmsBundle\Entity\term;

/**
 * @Route("/term")
 * @Template()
 */
class TermController extends BaseController
{
    /**
     * @Route("/")
     */
    public function indexAction()
    {
        $request = $this->get('request');

        $rows = max(1, $request->get('rows', 10));
        $page = max(1, $request->get('page', 1));

        $em  = $this->getDoctrine()->getManager();
        $ots = $em->getRepository('CmsBundle:term')->findBy([], ['pid'=>'ASC', 'id'=>'ASC']);

        $term = [];
        $this->enumTerm($term, $ots, 0, -1);
        $records = count($term);
        //
        return [
            'term'   => array_slice($term, ($page-1)*$rows, $rows),
            'records'=> $records,
            'page'   => $page,
            'total'  => ceil($records/$rows),
        ];
    }

    /**
     * @Route("/edit")
     */
    public function editAction()
    {
        $request = $this->get('request');
        $submit  = $request->get('submit');
        $tid     = $request->get('tid', 0);

        $em      = $this->getDoctrine()->getManager();
        $current = $tid>0 ? $em->find('CmsBundle:term', $tid) : new term();

        if ($submit) {
            $name        = $request->get('name');
            $identify    = $request->get('identify');
            $pid         = max(0, intval($request->get('pid')));
            $description = $request->get('description');

            if ($name=='') {
                return $this->msgOut('分类名称不能为空！', '/cms/term/edit');
            } elseif ($current->getName()!=$name) {
                if ($em->getRepository('CmsBundle:term')->findOneBy(['name'=>$name, 'pid'=>$pid])) {
                    return $this->msgOut('分类名称已存在！', '/cms/term/edit');
                }
            }

            if ($pid>0) {
                $pTerm = $em->find('CmsBundle:term', $pid);
                if (!$pTerm) {
                    return $this->msgOut('上一级分类不存在！', '/cms/term/edit');
                }
                $identify = $pTerm->getIdentify() . '_' . $identify;
            }

            if ($identify=='') {
                return $this->msgOut('唯一标识不能为空！', '/cms/term/edit');
            } elseif ($current->getIdentify()!=$identify) {
                if ($em->getRepository('CmsBundle:term')->findOneBy(['identify'=>$identify])) {
                    return $this->msgOut('唯一标识已存在！', '/cms/term/edit');
                }
            }

            $current
                ->setName($name)
                ->setIdentify($identify)
                ->setPid($pid)
                ->setDescription($description);

            $em->persist($current);
            $em->flush();

            if ($tid>0) {
                return $this->msgOut('修改分类成功!', '/cms/term/');
            }
            return $this->msgOut('添加分类成功！', '/cms/term/');
        }

        $identifyArr = explode('_', $current->getIdentify());
        $current->identify2 = array_pop($identifyArr);

        $ots = $em->getRepository('CmsBundle:term')->findBy([], ['pid'=>'ASC', 'id'=>'ASC']);

        $term = [];
        $this->enumTerm($term, $ots, 0, -1);

        return [
            'current' => $current,
            'term'    => $term,
        ];
    }

    /**
     * @Route("/delete")
     */
    public function deleteAction()
    {
        return array(
                // ...
            );    }

    /**
     * @Route("/recycle")
     */
    public function recycleAction()
    {
        return array(
            // ...
        );    }

    /**
     * @Route("/undo")
     */
    public function undoAction()
    {
        return array(
            // ...
        );    }

    /**
     * @Route("/delete2")
     */
    public function delete2Action()
    {
        return array(
                // ...
            );    }


}
